package com.mofeng.myshop.service.impl;

import com.mofeng.myshop.domain.dto.CommandDto;
import com.mofeng.myshop.domain.po.AddressMap;
import com.mofeng.myshop.domain.po.Command;
import com.mofeng.myshop.domain.vo.CommandVo;
import com.mofeng.myshop.mapper.AddressMapMapper;
import com.mofeng.myshop.mapper.CommandMapper;
import com.mofeng.myshop.service.ICommandService;
import com.mofeng.myshop.until.MyUtil;
import com.mofeng.myshop.until.R;
import com.mofeng.myshop.until.rcon.Rcon;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 管理员后台指令设置 服务实现类
 * </p>
 *
 * @author MoFeng
 * @since 2024-04-14
 */
@Service
@RequiredArgsConstructor
public class CommandServiceImpl implements ICommandService {

    private final CommandMapper commandMapper;

    private final AddressMapMapper addressMapMapper;

    /**
     * 管理员获取设置列表
     */
    @Override
    public R getListAdmin() {
        List<Command> commandList = commandMapper.findAll();
        return R.SUCCESS("命令列表查询成功", CommandVo.change(commandList));
    }

    /**
     * 新建或者修改
     */
    @Override
    public R updateCommand(CommandDto commandDto) {
        String commandId = commandDto.getId();
        Command commandDB = commandMapper.selectById(Long.parseLong(commandId));
        if (commandDB == null) {
            throw new RuntimeException("找不到指令 ");
        }
        Command command = commandDto.toCommand();
        commandMapper.updateById(command);
        return R.SUCCESS("命令修改成功");
    }

    /**
     * 添加管理员后台指令
     */
    @Override
    public R addCommand(CommandDto commandDto) {
        Command command = commandDto.toCommand();
        commandMapper.insert(command);
        return R.SUCCESS("命令添加成功");
    }

    /**
     * 删除配置
     */
    @Override
    public R delCommand(List<String> ids) {
        List<Long> idList = ids.stream().map(Long::parseLong).toList();
        commandMapper.deleteBatchIds(idList);
        return R.SUCCESS("命令删除成功");
    }

    /**
     * 执行命令
     */
    @Override
    public R startCommand(String commandId, String mapId) {
        Command command = commandMapper.selectById(Long.parseLong(commandId));
        if (command == null) {
            throw new RuntimeException("找不到指令 ");
        }

        AddressMap map = addressMapMapper.selectById(Long.parseLong(mapId));
        if (map == null) {
            throw new RuntimeException("找不到地图 ");
        }
        String name = map.getName();
        try {
            String domain = map.getIp();
            if (MyUtil.StringUtil.isEmpty(domain)) {
                throw new RuntimeException("地图ip不能为空");
            }
            int port = map.getRconPort();
            if (MyUtil.StringUtil.isEmpty(port)) {
                throw new RuntimeException("地图端口不能为空");
            }
            String password = map.getPassword();
            if (MyUtil.StringUtil.isEmpty(password)) {
                throw new RuntimeException("地图密码不能为空");
            }
            String ip = MyUtil.ServletUtil.getIPAddress(domain);
            Rcon rcon = new Rcon();
            rcon.connect(ip, port, password.getBytes());
            String result = rcon.command(command.getCommandValue());
            return R.SUCCESS("指令发送成功", result);
        } catch (Exception e) {
            return R.FAILED("服务器 “" + name + "” 指令发送失败，报错信息：" + e.getMessage());
        }
    }

}
